package cn.lili.modules.order.order.callback;


import cn.lili.common.vo.ResponseBean;
import cn.lili.exchange.AmqpExchangeProperties;
import cn.lili.modules.goods.entity.vos.yunxin.CallBackVO;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.modules.order.aftersale.service.AfterSaleService;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.order.order.service.OrderItemService;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.modules.order.trade.entity.enums.AfterSaleStatusEnum;
import cn.lili.util.AmqpSender;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 售后确认收货
 */
@Service("after_sales.receiving")
@Slf4j
public class AfterSalesReceivingService  implements MessageType{

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    @Lazy
    private OrderService orderService;

    @Autowired
    @Lazy
    private AfterSaleService afterSaleService;

    @Override
    public ResponseBean execute(CallBackVO callBackVO) {
        OrderItem orderItem = orderItemService.getOne(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getSupplyChainOrderId, callBackVO.getOrder_item_id()).last("limit 1"));

        AfterSale afterSale = afterSaleService.getOne(new LambdaQueryWrapper<AfterSale>().eq(AfterSale::getOrderItemSn, orderItem.getSn()).orderByDesc(AfterSale::getCreateTime).last("limit 1"));
        log.info("afterSale == {}", afterSale);
        if (afterSale == null) {
            return null;
        }
        afterSale.setServiceStatus(AfterSaleStatusEnum.SELLER_CONFIRM.name());
        afterSaleService.updateById(afterSale);

        return new ResponseBean(0, "售后确认收货", new HashMap<>());
    }
}
